'''
Created on 4.3.2011.

@author: Ana Banana
'''

'''
Created on Mar 1, 2011

@author: Mario
'''

from Bio import ExPASy
import re

import urllib

'''
Returns uniprot id of a protein based on its full name
It works by fetching and parsing the html page of the uniprot domain.
'''
def uniprot_id(desc):
    handle = ExPASy.sprot_search_ful(desc)      #"heat shock protein 90 10116"
    result = handle.read()

    p = re.compile('(http.*score)\n', re.DOTALL)
    if "Status: 302" in result:
        url = re.findall(p, result)
        url = re.sub(' ', '%20', url[0])

    sock = urllib.urlopen(url)
    htmlSource = sock.read()
    sock.close()

    #ako ima addOrAppendCart, onda je nasao barem jedan rezultat
    p = re.compile('"addOrAppendCart\(\'([\w]{6})')
    target_id = [];
    if "addOrAppendCart" in htmlSource:
        target_id = re.findall(p, htmlSource)
    if len(target_id) > 0:
        return target_id[0]          #trazeni rezultat
    else:
        return "";

